*Part 1. Import 2000-2016 CBP data
	forv yr = 2000/2016 {
	    if `yr'==2000{ 
			local i="00"
		}
		else if  `yr'>2000 & `yr'<2010{ 
			local i=`yr'-2000
			local i= "0`i'"
		}
		else if  `yr'>=2010{ 
			local i=`yr'-2000
		}
		qui{
		insheet using "$cbpdir/cbp`i'co.txt", clear
		gen year = `yr'	
		tempfile data`yr'
		save `data`yr''
		}
		}

	* Part 2. Append datasets
	clear
	forv yr = 2000/2016 {
		append using `data`yr''
		}
	
	order fipstate year
	sort fipstate year

*******************************************************************************
* Label CBP variables 
*******************************************************************************
	label var emp "Total Mid-March Employees with Noise"
	label var qp1 "Total First Quarter Payroll ($1,000) with Noise"
	label var ap "Total Annual Payroll ($1,000) with Noise"
	label var est "Total Number of Establishments"
	label var n1_4 "Number of Establishments: 1-4 Employee Size Class"
	label var n5_9 "Number of Establishments: 5-9 Employee Size Class"
	label var n10_19 "Number of Establishments: 10-19 Employee Size Class"
	label var n20_49 "Number of Establishments: 20-49 Employee Size Class"
	label var n50_99 "Number of Establishments: 50-99 Employee Size Class"
	label var n100_249 "Number of Establishments: 100-249 Employee Size Class"
	label var n250_499 "Number of Establishments: 250-499 Employee Size Class"
	label var n500_999 "Number of Establishments: 500-999 Employee Size Class"
	label var n1000 "Number of Establishments: 1,000 or More Employee Size Class"
	label var n1000_1 "Number of Establishments: 1,000-1,499 Employee Size Class"
	label var n1000_2 "Number of Establishments: 1,500-2,499 Employee Size Class"
	label var n1000_3 "Number of Establishments: 2,500-4,999 or More Employee Size Class"
	label var n1000_4 "Number of Establishments: 5,000 or More Employee Size Class"

drop *_nf

sa $datadir/CBP_county_ind, replace 

u $datadir/CBP_county_ind, clear

g n1_9 = n1_4 + n5_9
g n10_49 = n10_19+ n20_49
drop n1_4 n5_9 n10_19 n20_49 censtate cencty

label var n1_9 "Number of Establishments: 1-9 Employee Size Class"
label var n10_49 "Number of Establishments: 10-49 Employee Size Class"

rename n* est*
rename estaics naics

order fipstate year fipscty naics empflag emp qp ap est est1_ est10_ est50_ 
rename fipstate fips 

foreach v in emp ap qp1 {
replace `v'=. if empflag!=""
}

keep if substr(naics,-2,.)=="//" | substr(naics,-2,.)=="--"

rename naics industry
destring industry, gen(naics) ignore("/") force
destring industry, gen(temp) ignore("-") force
replace naics=temp if naics==.
drop industry temp

tempfile cbptemp
sa `cbptemp'

keep if inrange(naics,100,1000)
rename naics naics3
g naics2=floor(naics3/10)
recode naics2(33=31)
recode naics2(32=31)
recode naics2(45=44)
recode naics2(49=48)

rename (empflag emp qp1 ap) (empflag_n3 emp_n3 qp1_n3 ap_n3)

sa $datadir/CBP_county_ind_n3, replace

u `cbptemp', clear
keep if naics<100
rename naics naics2

rename (empflag emp qp1 ap) (empflag_n2 emp_n2 qp1_n2 ap_n2)

sa $datadir/CBP_county_ind_n2, replace

u `cbptemp', clear

keep if naics>1000 & naics!=. 
rename naics naics4
g naics3=floor(naics4/10)
g naics2=floor(naics3/10)
recode naics2(33=31)
recode naics2(32=31)
recode naics2(45=44)
recode naics2(49=48)

sa $datadir/CBP_county_ind, replace 

